package com.sky.controller.admin;

import com.sky.anotation.AllFull;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@Api(tags = "菜品分类类型接口")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
	@Autowired
	private CategoryService categoryService;

	/**
	 * 根据类型查询分类
	 * @param type
	 * @return
	 */
	@ApiOperation("根据类型查询分类")
	@GetMapping("/list")
	public Result list(Integer type) {
		List<Category> category = categoryService.list(type);
		return Result.success(category);
	}

	/**
	 * 分类分页查询
	 * @param categoryPageQueryDTO
	 * @return
	 */
	@ApiOperation("分类分页查询")
	@GetMapping("/page")
	public Result page(CategoryPageQueryDTO categoryPageQueryDTO){
		PageResult ret =  categoryService.page(categoryPageQueryDTO);
		return Result.success(ret);
	}

	/**
	 * 启用/禁用分类
	 * @param status
	 * @param id
	 * @return
	 */
	@ApiOperation("启用/禁用分类")
	@PostMapping("/status/{status}")
	public Result setStatus(@PathVariable Integer status, Long id){
		Category category = Category.builder()
									.id(id)
									.status(status)
									.build();
		log.info("setstatus:{}",category);
		categoryService.setStatus(category);;
		return Result.success();
	}

	/**
	 * 新增菜品分类
	 * @param categoryDTO
	 * @return
	 */
	@ApiOperation("新增菜品分类")
	@PostMapping
	public Result add(@RequestBody CategoryDTO categoryDTO){
		log.info("categoryDTO:{}",categoryDTO);
		categoryService.add(categoryDTO);
		return Result.success();
	}

	/**
	 * 根据id删除分类
	 * @param id
	 * @return
	 */
	@ApiOperation("根据id删除分类")
	@DeleteMapping
	public Result deleteById(Long id){
		categoryService.deleteById(id);
		return Result.success();
	}

	/**
	 * 修改菜品分类
	 * @param categoryDTO
	 * @return
	 */
	@ApiOperation("修改分类")
	@PutMapping
	public Result edit(@RequestBody CategoryDTO categoryDTO){
		log.info("修改分类:{}",categoryDTO);
		categoryService.edit(categoryDTO);
		return Result.success();
	}

}
